home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / vs / browser / cpb10eb.exe / DATA.Z / ufo.java < prev    next >
Text File  |  1996-09-25  |  4KB  |  181 lines

  1. // $Id: ufo.java,v 1.2 1996/08/26 12:15:07 sugino Exp $
  2. //
  3. // ufo.java
  4. // 
  5. // Copyright(C) 1996 Sony Corporation. All rights reserved.
  6. //
  7. import vrml.*;
  8. import vrml.field.*;
  9. import vrml.node.*;
  10.  
  11. public class ufo extends Script{
  12.     SFVec3f ufo_trans ;
  13.     SFBool setTimerEnabled ;
  14.     
  15.     int flg = 0;
  16.     int height = 0;
  17.     
  18.     public void initialize(){
  19.     ufo_trans = (SFVec3f) getEventOut("ufoTr");
  20.     setTimerEnabled = (SFBool) getEventOut( "setTimerEnabled" );
  21.     }
  22.  
  23.     public void processEvent(Event e){
  24.     String name = e.getName();
  25.     if(name.equals("clicked")){
  26.         clicked ( (ConstSFBool) e.getValue(), e.getTimeStamp()) ;
  27.     }
  28.     if(name.equals("move")){
  29.         move ( (ConstSFTime) e.getValue(), e.getTimeStamp()) ;
  30.     }
  31.     }
  32.  
  33.     public void clicked ( ConstSFBool ev, double time ){
  34.         // when the button is pressed, return immediately.
  35.         if( ev.getValue() == true ) return;
  36.         
  37.         // when the button is released...
  38.         switch ( flg ){
  39.           case ( 0 ):    // landing, set to rising
  40.             flg = 1;
  41.             start();
  42.             break;
  43.           case ( 2 ):    // flying, set to swinging
  44.             flg = 3;
  45.             start();
  46.             break;
  47.           case ( 3 ):    // swinging, set to falling
  48.             flg = 4;
  49.             break;
  50.           default:
  51.             break;
  52.         }
  53.     }
  54.  
  55.     public void move ( ConstSFTime ev, double time ){
  56.         switch ( flg ) {
  57.           case ( 1 ):
  58.             // rising
  59.             rise();
  60.             if( height >= 20 ){
  61.                 flg = 2;
  62.                 stop();
  63.             }
  64.             break;
  65.           case ( 3 ):
  66.             // swinging
  67.             swing();
  68.             break;
  69.           case ( 4 ):
  70.             // go back.
  71.             if(height > 0){
  72.                 gobackFall();
  73.             }else if(height < 0){
  74.                 gobackRise();
  75.             }
  76.             if( height == 0 ){
  77.                 flg = 0;
  78.                 stop();
  79.             }
  80.             break;
  81.           default:
  82.             break;
  83.         }
  84.     }
  85.  
  86.     float movepara[] = new float[3];
  87.     public void rise(){
  88.  
  89.         movepara[ 1 ] += 2.0f;
  90.         ufo_trans.setValue( movepara );
  91.         height = height + 2;
  92.     }
  93.  
  94.     public void gobackFall(){
  95.  
  96.         movepara[ 1 ] += -1.0f;
  97.         ufo_trans.setValue( movepara );
  98.         height = height - 1;
  99.     }
  100.  
  101.     public void gobackRise(){
  102.  
  103.         movepara[ 1 ] += 1.0f;
  104.         ufo_trans.setValue( movepara) ;
  105.         height = height + 1;
  106.     }
  107.  
  108.     public void swing(){
  109.         // random swinging.
  110.         int randomNum = (int)(6 * Math.random());
  111.         
  112.         switch(randomNum){
  113.           case 0:
  114.             swingLeft();
  115.             break;
  116.           case 1:
  117.             swingRight();
  118.             break;
  119.           case 2:
  120.             swingUp();
  121.             break;
  122.           case 3:
  123.             swingDown();
  124.             break;
  125.           case 4:
  126.             swingRise();
  127.             break;
  128.           case 5:
  129.             swingFall();
  130.             break;
  131.           default:
  132.             // do nothing.
  133.         }
  134.     }        
  135.             
  136.     public void swingLeft(){
  137.         movepara[ 0 ] += -2.0f;
  138.         ufo_trans.setValue( movepara ) ;
  139.     }
  140.  
  141.     public void swingRight(){
  142.         movepara[ 0 ] += 2.0f;
  143.         ufo_trans.setValue( movepara ) ;
  144.     }
  145.  
  146.     public void swingUp(){
  147.         movepara[ 1 ] += 2.0f;
  148.         ufo_trans.setValue( movepara ) ;
  149.         height = height + 2;
  150.     }
  151.  
  152.     public void swingDown(){
  153.         movepara[ 1 ] += -2.0f;
  154.         ufo_trans.setValue( movepara ) ; 
  155.         height = height - 2;
  156.     }
  157.  
  158.     public void swingRise(){
  159.         movepara[ 1 ] += 5.0f;
  160.         ufo_trans.setValue( movepara ) ;
  161.         height = height + 5;
  162.     }
  163.  
  164.     public void swingFall(){
  165.         movepara[ 1 ] += -5.0f;
  166.         ufo_trans.setValue( movepara ) ; 
  167.         height = height - 5;
  168.     }
  169.  
  170.     public void start(){
  171.         System.out.println("start() enter.");
  172.         setTimerEnabled.setValue( true );
  173.     }
  174.     public void stop(){
  175.         System.out.println("stop() enter.");
  176.         setTimerEnabled.setValue( false );
  177.     }
  178. }
  179.  
  180.  
  181.